﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace System.Collections.Generic
{
    /// <summary>
    /// 可以循环索引的链表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RingList<T> : IList<T>
    {
        public RingList()
        {
            this.list = new List<T>();
        }
        public RingList(IEnumerable<T> items)
        {
            this.list = new List<T>(items);
        }
        private List<T> list;

        public T this[int ringIndex]
        {
            get
            {
                var i = ringIndex % this.Count;
                if (i < 0)
                {
                    i += this.Count;
                }
                return this.list[i];
            }
            set
            {
                var i = ringIndex % this.Count;
                if (i < 0)
                {
                    i += this.Count;
                }
                this.list[i] = value;
            }
        }

        public int Count => list.Count;

        public bool IsReadOnly => false;

        public void Add(T item)
        {
            list.Add(item);
        }

        public void Clear()
        {
            list.Clear();
        }

        public bool Contains(T item)
        {
            return list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return list.GetEnumerator();
        }

        public int IndexOf(T item)
        {
            return list.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            list.Insert(index, item);
        }

        public bool Remove(T item)
        {
            return list.Remove(item);
        }

        public void RemoveAt(int index)
        {
            list.RemoveAt(index);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return list.GetEnumerator();
        }
    }
}
